using WorkflowCore.Interface;
using WorkflowCore.Models;
using WorkflowCore.Primitives;
using WorkflowApi.Models;

namespace WorkflowApi.Workflows
{
    public class ApprovalWorkflow : IWorkflow<WorkflowData>
    {
        public string Id => "ApprovalWorkflow";
        public int Version => 1;

        public void Build(IWorkflowBuilder<WorkflowData> builder)
        {
            builder
                .StartWith<StartStep>()
                .Then<ApprovalStep>()
                .Then<ConditionStep>()
                .When(data => data.NeedsManagerApproval)
                .Then<ManagerApprovalStep>()
                .Then<EndStep>()
                .When(data => !data.NeedsManagerApproval)
                .Then<EndStep>();
        }
    }

    public class WorkflowData
    {
        public string InstanceId { get; set; } = string.Empty;
        public string CurrentStep { get; set; } = string.Empty;
        public List<string> Approvers { get; set; } = new();
        public List<string> CcUsers { get; set; } = new();
        public bool NeedsManagerApproval { get; set; }
        public string Condition { get; set; } = string.Empty;
        public Dictionary<string, object> Properties { get; set; } = new();
    }

    public class StartStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            data!.CurrentStep = "Start";
            return ExecutionResult.Next();
        }
    }

    public class ApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            data!.CurrentStep = "Approval";
            
            // 检查是否有审批人
            if (data.Approvers.Any())
            {
                // 创建审批任务
                return ExecutionResult.WaitFor("ApprovalCompleted");
            }
            
            return ExecutionResult.Next();
        }
    }

    public class ManagerApprovalStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            data!.CurrentStep = "ManagerApproval";
            
            // 创建经理审批任务
            return ExecutionResult.WaitFor("ManagerApprovalCompleted");
        }
    }

    public class ConditionStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            data!.CurrentStep = "Condition";
            
            // 评估条件表达式
            if (!string.IsNullOrEmpty(data.Condition))
            {
                // 这里可以集成表达式引擎来评估条件
                data.NeedsManagerApproval = EvaluateCondition(data.Condition, data.Properties);
            }
            
            return ExecutionResult.Next();
        }

        private bool EvaluateCondition(string condition, Dictionary<string, object> properties)
        {
            // 简单的条件评估逻辑
            // 实际项目中可以使用表达式引擎如 DynamicExpresso
            return condition.Contains("amount") && properties.ContainsKey("amount") &&
                   Convert.ToDecimal(properties["amount"]) > 1000;
        }
    }

    public class EndStep : StepBody
    {
        public override ExecutionResult Run(IStepExecutionContext context)
        {
            var data = context.Workflow.Data as WorkflowData;
            data!.CurrentStep = "End";
            return ExecutionResult.Next();
        }
    }
} 